/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.bean.util.sale;

import cn.ekuma.epos.businesslogic.linecalc.DefaultLineCalcLogic;
import cn.ekuma.epos.businesslogic.linecalc.ServingLineCalcLogic;

import com.openbravo.format.Formats;
import com.openbravo.bean.shard.I_LineCalcLogic;
import com.openbravo.bean.sales.TicketLine;
import com.openbravo.pos.util.RoundUtils;
import com.openbravo.pos.util.StringUtils;

/**
 *
 * @author Administrator
 */
public class TicketLineHelper {
	static I_LineCalcLogic defaultLineCalcLogic=new DefaultLineCalcLogic();
    static I_LineCalcLogic serviceLineCalcLogic=new ServingLineCalcLogic();

     public static String printName(TicketLine l) {
        if(l.isPrefSell)
            return "*"+StringUtils.encodeXML(l.getProperty("product.name"));
        return StringUtils.encodeXML(l.getProperty("product.name"));
    }

    public static String printMultiply(TicketLine l) {
        return Formats.DOUBLE.formatValue(getMultiply(l));
    }

    public static String printPrice(TicketLine l) {
        return Formats.CURRENCY.formatValue(getRealPrice(l));
    }

    public static String printPriceTax(TicketLine l) {
        return Formats.CURRENCY.formatValue(getPriceTax(l));
    }

    public static String printTax(TicketLine l) {
        return Formats.CURRENCY.formatValue(getTax(l));
    }

    public static String printTaxRate(TicketLine l) {
        return Formats.PERCENT.formatValue(l.getTaxRate());
    }

    public static String printSubValue(TicketLine l) {
        return Formats.CURRENCY.formatValue(getSubValue(l));
    }

    public static String printValue(TicketLine l) {
        return Formats.CURRENCY.formatValue(getValue(l));
    }

    public static String printPriceRate(TicketLine l){
        return Formats.PERCENT.formatValue(l.priceRate);
    }

    public static String printDefSubValue(TicketLine l) {
        return Formats.CURRENCY.formatValue(getDefSubValue(l));
    }

    public static String printDefValue(TicketLine l) {
        return Formats.CURRENCY.formatValue(getDefValue(l));
    }
    
	public static double getDefTax(TicketLine l) {
		return RoundUtils.multiply(getDefSubValue(l) , l.getTaxRate());
	}

	public static double getDefValue(TicketLine l) {
		return RoundUtils.multiply(getDefSubValue(l) ,1.0 + l.getTaxRate());
	}

	public static double getDefSubValue(TicketLine l) {
		return RoundUtils.multiply(getRealDefaultPrice(l), l.getMultiply());
	}
    
    public static  double getPriceTax(TicketLine l) {
        return RoundUtils.multiply(getRealPrice(l) , 1.0 + l.getTaxRate());
    }

     public static double getSubValue(TicketLine l) {
        return RoundUtils.multiply(RoundUtils.multiply(getRealPrice(l) ,getMultiply(l)), l.priceRate);
    }

    public static double getTax(TicketLine l) {
        return RoundUtils.multiply(getSubValue(l) , l.getTaxRate());
    }

    public static double getPrefSubValue(TicketLine l){
        return RoundUtils.substract(RoundUtils.multiply(getRealDefaultPrice(l), getMultiply(l)),getSubValue(l));
    }

    public static  double getPrefValue(TicketLine l){
        return  RoundUtils.multiply(getPrefSubValue(l), l.getTaxRate());
    }

    public static double getValue(TicketLine l) {
        return RoundUtils.multiply(getSubValue(l) ,1.0 + l.getTaxRate());
    }

    public static I_LineCalcLogic getTicketLineCalcLogic(TicketLine l){
        if(l.isServiceTicketLine())
            return serviceLineCalcLogic;
        return defaultLineCalcLogic;
    }

    public static double getMultiply(TicketLine l) {
        return getTicketLineCalcLogic(l).getMultiply(l);
    }
    
    public static double getPrice(TicketLine l) {
        return l.getPrice();
    }

    public static void setPrice(TicketLine l, double m_dCustomerPrice) {
        l.setPrice(m_dCustomerPrice);
    }

    public static void setDefaultPrice(TicketLine l, double priceSell) {
        l.setDefaultPrice(priceSell);
    }

    public static void setPriceTax(TicketLine l,double priceTax){
    	l.setPrice(RoundUtils.divide(priceTax , 1.0 + l.getTaxRate()));
    }
    
    public static double getConsultCost(TicketLine line) {
	 return RoundUtils.multiply(line.getConsultCost() , getMultiply(line));
   }

	public static String printScore(TicketLine l) {
		return Formats.DOUBLE.formatValue(l.getScore());
	}

	public static double getScore(TicketLine line) {
		return RoundUtils.multiply(line.getScore(),getMultiply(line), line.priceRate);
	}
	
	public static double getRealPrice(TicketLine l) {
        return RoundUtils.add(l.getPrice(),l.getAttrPrice());
    }
	
	public static double getRealPriceCost(TicketLine l) {
        return RoundUtils.add(l.getConsultCost(),l.getAttrPriceCost());
    }
	
	public static double getRealDefaultPrice(TicketLine l) {
        return RoundUtils.add(l.getDefaultPrice(),l.getAttrPrice());
    }
}
